= 5 # assign 5 to the variable x
x = 10 # assign 10 to the variable y
y = x + y # add x and y, and assign the value to z
z print(z) # Output: 15
1 Python/Jupyter Crash Course
Some of you might have only little experience with Python and Jupyter notebooks. This introductory tutorial will help you to get started.
Jupyter notebooks
Jupyter notebooks are web-based interactive computing platforms that allow you to create and share documents that contain live code, equations, visualizations and narrative text. They are commonly used for data analysis, scientific computing, and machine learning. Here are some steps to get started with Jupyter notebooks:
Running code: In a Jupyter notebook, you can write and run code in cells. To run a cell, simply click on it and press
Shift + Enter
or click the “Run” button in the toolbar.Markdown cells: In addition to code cells, you can also create Markdown cells to write descriptive text. You can format the text using Markdown syntax to add headings, lists, links, etc..
Saving and exporting: To save your notebook, click on the “Save” button in the toolbar. Jupyter notebooks have the extension
.ipynb
and can be exported to other formats like HTML, PDF, etc.Keyboard Shortcuts: Jupyter notebooks have several keyboard shortcuts that can make your work faster and more efficient. For example, you can press
Esc
to enter command mode andEnter
to enter edit mode. The most frequently used Keyboard Shortcuts are:b
for creating a cellb
elow the current cell,a
for creating a cella
bove the current cell,dd
for deleting the current cell.
Google Colab
Colab allows you to use Jupyter notebooks with others without having to download, install, or run anything.
Introduction to Python
If you have not too much experience with Python programming, we strongly recommend to go through some online tutorials, such as the ones from Kaggle Learn. This will help you to get started. Here is just a brief overview of the basic concepts.
Variables
Variables are used to store values in a program. You can assign values to variables using the =
operator. For example (you can run it using SHIFT + ENTER
or clicking on Run
):
Data Types
Python has several built-in data types, including numbers (integer, float), strings, lists, tuples, and dictionaries. For example:
# Numbers
= 5
x = 5.5
y
# Strings
= "John Doe"
name
# Lists
= ["apple", "banana", "cherry"]
fruits
# Tuples
= ("John", "Doe", 30)
person
# Dictionaries
= {
person "first_name": "John",
"last_name": "Doe",
"age": 30
}
Operators
Python supports several types of operators, including arithmetic, comparison, and assignment operators. For example:
# Arithmetic
= 5
x = 3
y = x + y
z print(z) # Output: 8
# Comparison
= 5
x = 3
y print(x > y) # Output: True
# Assignment
= 5
x += 3
x print(x) # Output: 8
Conditional Statements
Conditional statements allow you to control the flow of a program based on certain conditions. You can play around by changing the value of x
.
= 5
x if x > 10:
print("x is greater than 10")
else:
print("x is less than or equal to 10")
Loops
Loops allow you to repeat a block of code multiple times. There are two types of loops in Python: for
and while
loops.
# For Loop
= ["apple", "banana", "cherry"]
fruits for fruit in fruits:
print(fruit)
# Output:
# apple
# banana
# cherry
# While Loop
= 0
x while x < 5:
print(x)
+= 1
x
# Output:
# 0
# 1
# 2
# 3
# 4
Functions
Functions are reusable blocks of code that perform a specific task. You can define functions using the def
keyword.
def greet(name):
print("Hello, " + name)
"Philippe") # Output: Hello, Philippe greet(
Additional examples
You can copy any of that code to make it run in a code
cell.
Numbers
# Addition
= 5
x = 10
y = x + y
z print(z) # Output: 15
# Subtraction
= 15
x = 10
y = x - y
z print(z) # Output: 5
# Multiplication
= 5
x = 10
y = x * y
z print(z) # Output: 50
# Division
= 15
x = 10
y = x / y
z print(z) # Output: 1.5
# Modulo
= 15
x = 10
y = x % y
z print(z) # Output: 5
# Exponentiation
= 5
x = 2
y = x ** y
z print(z) # Output: 25
Just create a code
cell below to run selected examples.
Strings
= "John"
first_name = "Smith"
last_name = first_name + " " + last_name
full_name print(full_name) # Output: John Smith
# Repetition
= "hello"
x = x * 3
y print(y) # Output: hellohellohello
# Indexing
= "hello"
x print(x[0]) # First element -> Output: h
print(x[-1]) # Last element -> Output: o
# Slicing
= "hello"
x print(x[1:4]) # Output: ell
print(x[:3]) # Output: hel
print(x[1:]) # Output: ello
# Membership
= "hello"
x print("h" in x) # Output: True
print("z" in x) # Output: False
Lists
# Indexing
= ["apple", "banana", "cherry"]
fruits print(fruits[0]) # Output: apple
print(fruits[-1]) # Output: cherry
# Slicing
= ["apple", "banana", "cherry"]
fruits print(fruits[1:3]) # Output: ['banana', 'cherry']
# Membership
= ["apple", "banana", "cherry"]
fruits print("apple" in fruits) # Output: True
print("orange" in fruits) # Output: False
# Length
= ["apple", "banana", "cherry"]
fruits print(len(fruits)) # Output: 3
# Append
= ["apple", "banana", "cherry"]
fruits "orange")
fruits.append(print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']
# Insert
= ["apple", "banana", "cherry"]
fruits 1, "orange")
fruits.insert(print(fruits) # Output: ['apple', 'orange', 'banana', 'cherry']
# Remove
= ["apple", "banana", "cherry"]
fruits "banana")
fruits.remove(print(fruits) # Output: ['apple', 'cherry']
# Pop
= ["apple", "banana", "cherry"]
fruits = fruits.pop()
x print(x) # Output: cherry
print(fruits) # Output: ['apple', 'banana']
Tuples
# Indexing
= ("John", "Doe", 30)
person print(person[0]) # Output: John
print(person[-1]) # Output: 30
# Slicing
= ("John", "Doe", 30)
person print(person[1:3]) # Output: ('Doe', 30)
# Membership
= ("John", "Doe", 30)
person print("John" in person) # Output: True
print("Jane" in person) # Output: False
# Length
= ("John", "Doe", 30)
person print(len(person)) # Output: 3
Dictionaries
# Indexing
= {
person "first_name": "John",
"last
last_name": "Doe",
"age": 30
}print(person["first_name"]) # Output: John
# Membership
= {
person "first_name": "John",
"last_name": "Doe",
"age": 30
}print("first_name" in person) # Output: True
print("email" in person) # Output: False
# Length
= {
person "first_name": "John",
"last_name": "Doe",
"age": 30
}print(len(person)) # Output: 3
# Adding key-value pairs
= {
person "first_name": "John",
"last_name": "Doe",
"age": 30
}"email"] = "johndoe@email.com"
person[print(person) # Output: {'first_name': 'John', 'last_name': 'Doe', 'age': 30, 'email': 'johndoe@email.com'}
# Modifying values
= {
person "first_name": "John",
"last_name": "Doe",
"age": 30
}"first_name"] = "Jane"
person[print(person) # Output: {'first_name': 'Jane', 'last_name': 'Doe', 'age': 30}
# Removing key-value pairs
= {
person "first_name": "John",
"last_name": "Doe",
"age": 30
}del person["age"]
print(person) # Output: {'first_name': 'John', 'last_name': 'Doe'}
This is just a brief introduction to Python programming. There are many more advanced topics and features in Python, but these basics should give you a good start.
If you want more exercises for basic Python Programming go through the Intro to Programming
and Python
courses on Kaggle Learn.